﻿using Md.Api.DDD;
using Md.Redis.Repositories;
using Microsoft.Extensions.DependencyInjection;

namespace Md.Core.Moddlewares;
/// <summary>
/// redis注册
/// </summary>
public static class RedisSetup
{
	public static void AddRedisSetup(this IServiceCollection services)
	{
        services.AddSingleton(c => new MdRedisService(GetRedisOption()));
	}

    /// <summary>
    /// 获取配置
    /// </summary>
    /// <param name="configuration"></param>
    /// <param name="clientSideCacheKeyFilter"></param>
    /// <returns></returns>
    static MdRedisOption GetRedisOption()
    {
        var redisConnStr = JsonConfig.JsonRead("MdRedis:MdConnection");

        var redisConnParamList = redisConnStr.Split(",");

        var RedisHost = redisConnParamList[0].Split(":")[0];

        var RedisPassword = redisConnParamList[1].Split("=")[1];

        var RedisPort = Convert.ToInt32(redisConnParamList[0].Split(":")[1]);

        var SyncTimeout = Convert.ToInt32(redisConnParamList[4].Split("=")[1]);

        var ConnectTimeout = Convert.ToInt32(redisConnParamList[2].Split("=")[1]);

        var DefaultIndex = Convert.ToInt32(redisConnParamList[5].Split("=")[1]);

        return new MdRedisOption()
        {
            RedisHost = RedisHost,
            RedisPassword = RedisPassword,
            RedisPort = RedisPort,
            SyncTimeout = SyncTimeout,
            ConnectTimeout = ConnectTimeout,
            DefaultIndex = DefaultIndex,
            Poolsize = 5,
            UseClientSideCache = Convert.ToBoolean(JsonConfig.JsonRead("MdRedis:UseCache"))
        };
    }
}

